home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrecord-1.8.1 / misc / io.c next >
Encoding:
C/C++ Source or Header  |  1999-12-24  |  5.1 KB  |  219 lines

  1. /* @(#)io.c    1.19 98/10/10 Copyright 1988 J. Schilling */
  2. #ifndef lint
  3. static    char sccsid[] =
  4.     "@(#)io.c    1.19 98/10/10 Copyright 1988 J. Schilling";
  5. #endif
  6. /*
  7.  *    Copyright (c) 1988 J. Schilling
  8.  */
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2, or (at your option)
  13.  * any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #include <mconfig.h>
  26. #include <stdio.h>
  27. #include <standard.h>
  28. #include <ctype.h>
  29. #include <vadefs.h>
  30. #include <stdxlib.h>
  31. #include <strdefs.h>
  32. #include <utypes.h>
  33.  
  34. struct disk {
  35.     int    dummy;
  36. };
  37.  
  38. LOCAL    char    *skipwhite    __PR((const char *));
  39. LOCAL    void    prt_std        __PR((char *, long, long, long, struct disk *));
  40. EXPORT    BOOL    cvt_std        __PR((char *, long *, long, long, struct disk *));
  41. extern    BOOL    getvalue    __PR((char *, long *, long, long,
  42.                 void (*)(char *, long, long, long, struct disk *),
  43.                 BOOL (*)(char *, long *, long, long, struct disk *),
  44.                 struct disk *));
  45. extern    BOOL    getlong        __PR((char *, long *, long, long));
  46. extern    BOOL    getint        __PR((char *, int *, int, int));
  47. extern    BOOL    yes        __PR((char *, ...));
  48.  
  49. LOCAL
  50. char *skipwhite(s)
  51.          const char    *s;
  52. {
  53.     register const Uchar    *p = (const Uchar *)s;
  54.  
  55.     while (*p) {
  56.         if (!isspace(*p))
  57.             break;
  58.         p++;
  59.     }
  60.     return ((char *)p);
  61. }
  62.  
  63. /* ARGSUSED */
  64. EXPORT
  65. BOOL
  66. cvt_std(linep, lp, mini, maxi, dp)
  67.     char    *linep;
  68.     long    *lp;
  69.     long    mini;
  70.     long    maxi;
  71.     struct disk    *dp;
  72. {
  73.     long    l    = -1L;
  74.  
  75. /*    printf("cvt_std(\"%s\", %d, %d, %d);\n", linep, *lp, mini, maxi);*/
  76.  
  77.     if (linep[0] == '?') {
  78.         printf("Enter a number in the range from %ld to %ld\n",
  79.                                 mini, maxi);
  80.         printf("The default radix is 10\n");
  81.         printf("Precede number with '0x' for hexadecimal or with '0' for octal\n");
  82.         printf("Shorthands are:\n");
  83.         printf("\t'^' for minimum value (%ld)\n", mini);
  84.         printf("\t'$' for maximum value (%ld)\n", maxi);
  85.         printf("\t'+' for incrementing value to %ld\n", *lp + 1);
  86.         printf("\t'-' for decrementing value to %ld\n", *lp - 1);
  87.         return (FALSE);
  88.     }
  89.     if (linep[0] == '^' && *skipwhite(&linep[1]) == '\0') {
  90.         l = mini;
  91.     } else if (linep[0] == '$' && *skipwhite(&linep[1]) == '\0') {
  92.         l = maxi;
  93.     } else if (linep[0] == '+' && *skipwhite(&linep[1]) == '\0') {
  94.         if (*lp < maxi)
  95.             l = *lp + 1;
  96.     } else if (linep[0] == '-' && *skipwhite(&linep[1]) == '\0') {
  97.         if (*lp > mini)
  98.             l = *lp - 1;
  99.     } else if (*astol(linep, &l)) {
  100.         printf("Not a number: '%s'.\n", linep);
  101.         return (FALSE);
  102.     }
  103.     if (l < mini || l > maxi) {
  104.         printf("'%s' is out of range.\n", linep);
  105.         return (FALSE);
  106.     }
  107.     *lp = l;
  108.     return (TRUE);
  109. }
  110.  
  111. /* ARGSUSED */
  112. LOCAL void
  113. prt_std(s, l, mini, maxi, dp)
  114.     char    *s;
  115.     long    l;
  116.     long    mini;
  117.     long    maxi;
  118.     struct disk *dp;
  119. {
  120.     printf("%s %ld (%ld - %ld)/<cr>:", s, l, mini, maxi);
  121. }
  122.  
  123. EXPORT
  124. BOOL getvalue(s, lp, mini, maxi, prt, cvt, dp)
  125.     char    *s;
  126.     long    *lp;
  127.     long    mini;
  128.     long    maxi;
  129.     void    (*prt) __PR((char *, long, long, long, struct disk *));
  130.     BOOL    (*cvt) __PR((char *, long *, long, long, struct disk *));
  131.     struct disk    *dp;
  132. {
  133.     char    line[128];
  134.     char    *linep;
  135.  
  136.     for(;;) {
  137.         (*prt)(s, *lp, mini, maxi, dp);
  138.         flush();
  139.         line[0] = '\0';
  140.         if (getline(line, 80) == EOF)
  141.             exit(EX_BAD);
  142.  
  143.         linep = skipwhite(line);
  144.         /*
  145.          * Nicht initialisierte Variablen
  146.          * duerfen nicht uebernommen werden
  147.          */
  148.         if (linep[0] == '\0' && *lp != -1L)
  149.             return (FALSE);
  150.  
  151.         if (strlen(linep) == 0) {
  152.             /* Leere Eingabe */
  153.         } else if ((*cvt)(linep, lp, mini, maxi, dp))
  154.             return (TRUE);
  155.     }
  156.     /* NOTREACHED */
  157. }
  158.  
  159. EXPORT
  160. BOOL getlong(s, lp, mini, maxi)
  161.     char    *s;
  162.     long    *lp;
  163.     long    mini;
  164.     long    maxi;
  165. {
  166.     return (getvalue(s, lp, mini, maxi, prt_std, cvt_std, (void *)0));
  167. }
  168.  
  169. EXPORT
  170. BOOL getint(s, ip, mini, maxi)
  171.     char    *s;
  172.     int    *ip;
  173.     int    mini;
  174.     int    maxi;
  175. {
  176.     long    l = *ip;
  177.     BOOL    ret;
  178.  
  179.     ret = getlong(s, &l, (long)mini, (long)maxi);
  180.     *ip = l;
  181.     return (ret);
  182. }
  183.  
  184. /* VARARGS1 */
  185. #ifdef    PROTOTYPES
  186. EXPORT BOOL yes(char *form, ...)
  187. #else
  188. EXPORT
  189. BOOL yes(form, va_alist)
  190.     char    *form;
  191.     va_dcl
  192. #endif
  193. {
  194.     va_list    args;
  195.     char okbuf[10];
  196.  
  197. again:
  198. #ifdef    PROTOTYPES
  199.     va_start(args, form);
  200. #else
  201.     va_start(args);
  202. #endif
  203.     printf("%r", form, args);
  204.     va_end(args);
  205.     flush();
  206.     if (getline(okbuf, sizeof(okbuf)) == EOF)
  207.         exit(EX_BAD);
  208.     if (okbuf[0] == '?') {
  209.         printf("Enter 'y', 'Y', 'yes' or 'YES' if you agree with the previous asked question.\n");
  210.         printf("All other input will be handled as if the question has beed answered with 'no'.\n");
  211.         goto again;
  212.     }
  213.     if(streql(okbuf, "y") || streql(okbuf, "yes") ||
  214.        streql(okbuf, "Y") || streql(okbuf, "YES"))
  215.         return(TRUE);
  216.     else
  217.         return(FALSE);
  218. }
  219.